/*
 * @Author: your name
 * @Date: 2021-06-12 18:05:09
 * @LastEditTime: 2021-06-12 21:24:40
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \第02遍\assets\js\login.js
 */
$(function () {
    $("#loginBox a").on('click',function () {
        $('#loginBox').hide()
        $("#registerBox").show()
    })
    $("#registerBox a").on('click',function () {
        $('#registerBox').hide()
        $("#loginBox").show()
    })


let form = layui.form

form.verify({
    pwd: [
        /^[\S]{6,15}$/,
        '密码的长度为6-15个字符，且不能包含空格'
    ],
    repwd: function (value) {
        let pwd = $("#registerBox [name=password]").val()
        if (value !== pwd) {
            return '输出的两次密码不一致！'
        }
    }

})
    
    let layer = layui.layer
    $("#form-reg").on("submit",function (e) {
        e.preventDefault();
       
        axios({
            method: 'POST',
            url: '/api/reguser',
            data:$('#form-reg').serialize()
            
        }).then((res) => {
            console.log(res);
            if (res.data.status !== 0) {
                return layer(res.data.message)
            }
            layer.msg('恭喜您，注册用户成功！')
            $("#form-reg")[0].reset()
            $("#registerBox a")[0].click()
        })
    })

    $("#form-login").on('submit',function (e) {
        e.preventDefault()
        axios({
            method: 'POST',
            url: '/api/login',
            data:$("#form-login").serialize()
        }).then((res) => {
            let obj = res.data
            if (obj.status !== 0) {
                return layer.msg(obj.message)
            }

            layer.msg('恭喜您，登录成功')
            location.href = '/index.html'
            localStorage.setItem('token',obj.token)
        })
    })







})